package com.wzdigit.wms.raw.controller.gdn;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.transfer.SearchGdnDtlRequest;
import com.wzdigit.wms.basic.client.request.transfer.SearchGdnHeaderRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.transfer.GdnDtlGoodsListResponse;
import com.wzdigit.wms.basic.client.response.transfer.GdnDtlListResponse;
import com.wzdigit.wms.basic.client.response.transfer.GdnHeaderListResponse;
import com.wzdigit.wms.raw.client.request.asn.report.asnreceive.AsnReceiveSearchRequest;
import com.wzdigit.wms.raw.client.response.gdn.report.GdnHeaderListNewResponse;
import com.wzdigit.wms.raw.client.response.gdn.report.RawGdnEnums;
import com.wzdigit.wms.raw.constant.RawWareConstant;
import com.wzdigit.wms.raw.service.gdn.GdnService;
import io.swagger.annotations.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.List;

@RestController
@Api(tags = "出库单查询")
@RequestMapping(value = RawWareConstant.API_VERSION)
public class GdnController {
    public static final String REQUEST_MODEL = "/gdn";

    @Autowired
    GdnService gdnService ;
    /**
     * 发货单列表
     * @param dto
     * @param servletResponse
     * @return 分页对象
     */
    @ApiOperation(value = "发货单列表",notes = "发货单列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/listHeader")
    public PagerInfo<List<GdnHeaderListNewResponse>> listGdnHeader(
            SearchGdnHeaderRequest dto, Pager<?> pager,
            HttpServletResponse servletResponse) {
        try {
            return gdnService.listGdnHeader(dto, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }



    /**
     * 发货单明细列表
     * @param dto
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "发货单明细列表",notes = "发货单列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/listDtl")
    public PagerInfo<List<GdnDtlListResponse>> listGdnDtl(
            SearchGdnDtlRequest dto, Pager<?> pager,
            HttpServletResponse servletResponse) {
        try {
            return gdnService.listGdnDtl(dto, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    /**
     * 发货单明细SN列表
     * @param gdnDtlId
     * @param pager
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "发货单明细SN列表",notes = "发货单明细SN列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/listDtlGoods")
    public <T> PagerInfo<List<GdnDtlGoodsListResponse>> searchGdnDtlGoods(
            @ApiParam(value = "发货单明细ID", required = true)
            @RequestParam(required = true) Integer gdnDtlId,
            Pager<T> pager,
            HttpServletResponse servletResponse) {
        try {
            return gdnService.searchGdnDtlGoods(gdnDtlId, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }


    @ApiOperation(value = "发货单明细行号+物料编码",notes = "查询发货单明细列行号和物料编码")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/listGdnDtlLineNoAndSkuCode/{gdnHeaderId}")
    public List<GdnDtlListResponse> listGdnDtlLineNoAndSkuCode(
            @ApiParam("发货单主表ID")
            @PathVariable("gdnHeaderId") Integer gdnHeaderId,
            HttpServletResponse servletResponse) {
        try {
            return gdnService.listGdnDtlLineNoAndSkuCode(gdnHeaderId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }


    @ApiOperation(value = "发货单导出", notes = "发货单导出")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/export", method = RequestMethod.POST)
    public void exportOrtherAsnReport(
            @RequestBody  SearchGdnHeaderRequest dto,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception{
        OutputStream outputStream =null;
        try {
//            XSSFWorkbook workbook =  gdnService.exportGdn(dto);
            XSSFWorkbook workbook =  gdnService.exportGdnNew(dto);

            servletResponse.setContentType("application/octet-stream");
            String userAgent = servletRequest.getHeader("USER-AGENT");
            String fileName = "工单产量查询";

            if (userAgent.contains("Mozilla")) {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } else {
                fileName = URLEncoder.encode(fileName, "utf8");
            }
            servletResponse.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xlsx");
            servletResponse.flushBuffer();
            outputStream = servletResponse.getOutputStream();
            workbook.write(outputStream);
            outputStream.flush();
        } catch (Exception e) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }finally {
            outputStream.close();
        }
    }

    @ApiOperation(value = "取消出库单", notes = "取消出库单")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/CancelGdn", method = RequestMethod.POST)
    public Result<?> scanUpCommit (
            @RequestBody List<BigDecimal> gdnHeadersList,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception{
        try{
            return gdnService.cacnelRawGdn(gdnHeadersList,servletRequest,servletResponse);
        }catch (Exception ex) {
            ex.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    @ApiOperation(value = "单据类型下拉框", notes = "单据类型下拉框")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/getGdnEnumsList", method = RequestMethod.GET)
    public List<RawGdnEnums> getEnums(
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception{
        try {
            return gdnService.getGdnEnumsList();
        } catch (Exception e) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

}
